﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace QLTV_GiaoDien
{
    public partial class frm_ViTri : DevExpress.XtraEditors.XtraForm
    {
        public frm_ViTri()
        {
            InitializeComponent();
        }
        private int _flag;

        public int Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }
        private int _MaViTri;

        public int MaViTri
        {
            get { return _MaViTri; }
            set { _MaViTri = value; }
        }
        private string _TenViTri;

        public string TenViTri
        {
            get { return _TenViTri; }
            set { _TenViTri = value; }
        }
        private string _MoTa;

        public string MoTa
        {
            get { return _MoTa; }
            set { _MoTa = value; }
        }
        public void EnabledTextFalse(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Enabled = false;
            }
            foreach (Control i in ctr.Controls)
                EnabledTextFalse(i);
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Text =string.Empty;
            }
            foreach (Control i in ctr.Controls)
                Huy_Text(i);
        }
        public void GetData()
        {
            txt_MaViTri.Text = MaViTri.ToString();
            txt_TenVT.Text = TenViTri;
            txt_MoTa.Text = MoTa;
        }
        public delegate void GetInt(int kt);
        public GetInt MyGetData;
        public void LoadForm()
        {
            if (Flag == 0)
            {
                this.Text = "Thêm Vị Trí Lưu Trữ";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
            }
            if (Flag == 1)
            {
                this.Text = "Sửa Vị Trí Lưu Trữ";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                GetData();
            }
            if (Flag == 2)
            {
                this.Text = "Xem Vị Trí Lưu Trữ";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                bbi_Sua.Enabled = false;
                EnabledTextFalse(this);
                GetData();
            }
        }
        ViTri_BLL vt_bll = new ViTri_BLL();

        private void frm_ViTri_Load(object sender, EventArgs e)
        {
            LoadForm();

        }
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ViTriLuuTru_Public vt = new ViTriLuuTru_Public();
            if (Flag == 0)
            {
                //them
                vt.TenViTri = txt_TenVT.Text;
                vt.MoTa = txt_MoTa.Text;
                vt_bll.insert_viTri(vt,vt);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Thêm";
                nk.MoTa = "Thêm mới Vị trí lưu trữ: '" + txt_TenVT.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (Flag ==1)
            {
                //sua
                vt.MaViTri = Convert.ToInt32(txt_MaViTri.Text);
                vt.TenViTri = txt_TenVT.Text;
                vt.MoTa = txt_MoTa.Text;
                vt_bll.update_viTri(vt,vt, vt);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Vị trí lưu trữ: '" +TenViTri.ToString() + "' thành: '" + txt_TenVT.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (Flag == 2)
            {
                //xem
                vt.MaViTri = Convert.ToInt32(txt_MaViTri.Text);
                vt.TenViTri = txt_TenVT.Text;
                vt.MoTa = txt_MoTa.Text;
                vt_bll.update_viTri(vt, vt, vt);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Vị trí lưu trữ: '" + TenViTri.ToString() + "' thành: '" + txt_TenVT.Text+ "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            MyGetData(1);
            Huy_Text(this);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Huy_Text(this);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
            Flag = 4;
        }

        private void bbi_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void txt_MaViTri_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txt_MaViTri_Leave(object sender, EventArgs e)
        {
            if (txt_MaViTri.Text == "")
                txt_MaViTri.Focus();
        }

        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 0;
            this.Text = "Thêm Mới Vị Trí";
            txt_MaViTri.Enabled = false;
            txt_MaViTri.Text = "Mã tự sinh";
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
        }

        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 1;
            txt_MaViTri.Enabled = true;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            this.Text = "Sửa Vị Trí";
        }
    }
}